from leetcode_test import ListNode


class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        p1 = p2 = head
        for i in range(n):
            p2 = p2.next
        while p2 and p2.next:
            p1 = p1.next
            p2 = p2.next

        if p2:
            p1.next = p1.next.next
        else:
            return head.next
        return head
